package com.hnkywl.sts.action.sys;

import com.hnkywl.sts.entity.sys.Gw;
import com.hnkywl.sts.service.sys.GwService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sys
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/sys/gw")
public class GwAction extends SimpleActionSupport {
    private GwService gwService;
    //岗位
    private Gw gw;
    //岗位名称
    private String mc;
    //岗位Id
    private Long gwId;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows=10;
    //行索引
    private int index;

    //岗位的查询分页
    @Action("gwList")
    public void gwList() {
        this.renderJson(this.gwService.findAllGw(mc, page, rows));
    }

    //获取岗位名称列表
    @Action("gwmcList")
    public void gwmcList() {
        this.renderJson(this.gwService.findGwMc());
    }

    @Action("gw_content")
    public String gwContent() {
        //进入update页面
        if (gwId != null){
            gw = gwService.findGwDetail(gwId);
        }else{
            //进入add页面
            gw = new Gw();
            //新增时自动生成排序号
            gw.setPxh(this.gwService.findNextPxh());
        }
        return SUCCESS;
    }

    /**
     * 方法名:     添加岗位
     * 功能描述:      向数据库添加岗位
     * 输入参数:    <@param gw 岗位>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-7上午9:47
     * 修改人:     zsp
     * 修改日期:   13-3-7上午9:47
     */
    @Action("add_gw")
    public void add() {
        try {
            this.gwService.saveGw(gw);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存岗位信息成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     删除修改岗位
     * 功能描述:      删除数据库岗位
     * 输入参数:    <@param gwId 岗位Id>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-7上午11:47
     * 修改人:     zsp
     * 修改日期:   13-3-7上午11:47
     */
    @Action("delete_gw")
    public void delete() {
        try {
            this.gwService.deleteGw(gwId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除岗位信息成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!gwService.ajaxValidate(gwId,validateVal,validateField))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    @Autowired
    public void setGwService(GwService gwService) {
        this.gwService = gwService;
    }

    //============getter setter===========
    public String getMc() {
        return mc;
    }

    public void setMc(String mc) {
        this.mc = mc;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getGwId() {
        return gwId;
    }

    public void setGwId(Long gwId) {
        this.gwId = gwId;
    }

    public Gw getGw() {
        return gw;
    }

    public void setGw(Gw gw) {
        this.gw = gw;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
